-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Feat(rerank): Add Cohere Reranker with topN filtering and fallback support #3791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
767a1cb
to
b2aa03a
Compare
- Implemented rerank logic using Cohere Rerank API with WebClient Signed-off-by: nirsa <[email protected]>
- Register a default DocumentPostProcessor that returns documents as-is Signed-off-by: nirsa <[email protected]>
exception - Changed setRelevanceScore parameter from int to double - Prevented IllegalFormatConversionException when formatting with %.4f Signed-off-by: nirsa <[email protected]>
Signed-off-by: nirsa <[email protected]>
b2aa03a
to
1906c1f
Compare
Hi! In my opinion, SpringAI is designed to follow interface-oriented programming and external modifications. We should design a rerank interface and design a unified request and response body to implement rerank. |
} | ||
|
||
this.webClient = WebClient.builder() | ||
.baseUrl(COHERE_RERANK_ENDPOINT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a feature that allows users to specify the URL address?
* Represents a single reranked document result returned by the Cohere API. Contains | ||
* the original index and the computed relevance score. | ||
*/ | ||
public static class Result { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to use a record class? Just a personal suggestion.
*/ | ||
private int extractTopN(Query query) { | ||
Object value = query.context().get("topN"); | ||
return (value instanceof Number num) ? num.intValue() : 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should expose the default value as a public property for external access, instead of using a magic value here.
@@ -0,0 +1,116 @@ | |||
package org.springframework.ai.rag.postretrieval.rerank; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a license header to the top of all newly added classes.
Summary
This PR introduces a new DocumentPostProcessor implementation that leverages the Cohere Rerank API to reorder retrieved documents based on semantic relevance.
It enables post-retrieval reranking using the Query input and relevance scores returned by the Cohere API.
Key Features
Configuration & Behavior Notes
The rerank feature is only activated when the following properties are present in your configuration
If omitted or disabled, rerank is skipped, and no HTTP calls to Cohere will be made.
Controlling topN Results
If "topN" is not provided in the Query.context() or is invalid, it defaults to 3
Default behavior without topN
Custom topN usage
This ensures the rerank feature remains optional, configurable, and backward-compatible.
Usage Example
Result
Notes